/*
brush.h
offer brush.c function api, data typedef
designed by lunanting

-------------------------
2020-03-25 17:51:38
create file
-------------------------
*/
#ifndef __BRUSH_H
#define __BRUSH_H

#include "../../bsp/bsp.h"
#include "aos/kernel.h"


//modbus function type
#define BRUSH_WRITE mb_hold_w
#define BRUSH_READ  mb_hold_r

//brush motor cmd register
#define BRUSH_RUN       0x2000  //brush run
#define BRUSH_HZ        0x2001  //brush fre
#define BRUSH_RESET     0x2002  //brush reset
#define BRUSH_CODE      0x2100  //brush error/warn code
#define BRUSH_STATE     0x2101  //brush state require
#define BRUSH_CURRENT   0x2104  //brush current
#define BRUSH_TORQUE    0x210B  //bursh torque percent
#define BRUSH_SPEED     0x210c  //brush actual speed


#define BRUSH_MAX      (20)     //记录电流的值的数组的长度
/*********************data type define*********************/
//brush motor run type
typedef enum{
    brush_run_no=0,
    brush_run_start,
    brush_run_stop,
    brush_run_jog
}BRUSH_run;

//brush motor dir
typedef enum{
    brush_dir_no=0,
    brush_dir_a,    //正转 
    brush_dir_b,    //反转
    brush_dir_change
}BRUSH_dir;

//brush motor exp /mod
typedef enum{
    brush_exp_1=0,
    brush_exp_2,
    brush_exp_3,
    brush_exp_4,
}BRUSH_exp;

//brush motor speed choose
typedef enum{
    brush_speed_master=0,
    brush_speed_1,
    brush_speed_2,
    brush_speed_3,
}BRUSH_speed;


//brush motor run state
typedef struct{
    int run;    //run state,0=stop,1=ruun,2=jog
    int dir;    //run direction,-2=change,-1=reverse,0=no dir,1=forward
    int exp;    //run expedite choose,1=first....1~4
    int speed;  //run speed choose 0=default,1~5
    int read_current;   //read current flag
    float current;  //current value
    uint16_t run_data;    //run config data
    uint16_t hz_data; //frequency config
    uint16_t reset_data;    //reset
    uint8_t read_state[10]; //read data
    uint8_t read_len;  //read data length
    MB_data modbus; //modbus config
    uint8_t modbus_port;    //modbus port
	u64 run_timestamp;
	int press_stop;
    uint8_t init;   //init state
    aos_mutex_t mux;
}BRUSH_state;

//brush motor run package type
typedef struct{
    BRUSH_run run;  //run type
    BRUSH_dir dir;  //dir type
    BRUSH_exp exp;  //exp/mod choose
    BRUSH_speed speed;  //speed choose
    uint8_t choose_en;  //choose enable state
}BRUSH_conf;






/*************************function extern*************************/
extern void xp_brush_run_config(BRUSH_run run); //brush motor config run state
extern void xp_brush_dir_config(BRUSH_dir dir); //brush motor config dir
extern void xp_brush_exp_config(BRUSH_exp exp); //brush motor config expedit/moderate
extern void xp_brush_speed_config(BRUSH_speed speed);   //brush motor config speed choose
extern void xp_brush_choose_enable(uint8_t state);  //brush motor choose enable

extern int xp_brush_run_choose(BRUSH_run run,BRUSH_dir dir,BRUSH_exp exp,BRUSH_speed speed);   //brush motor run with choose
extern int xp_brush_run(BRUSH_run run,BRUSH_dir dir);  //brush motor run without choose
extern int xp_brush_run_pack(BRUSH_conf *conf); //brush motor run use package
extern int xp_brush_set_hz(float hz); //brush motor set hz
extern int xp_brush_reset(void);    //brush motor reset

extern int xp_brush_stop(void); //set bursh motor stop
extern int xp_brush_foreward(void); //set brush motor  正转
extern int xp_brush_reversal(void); //set brush motor 反转
extern int xp_brush_cmd_set(int cmd);   //brush set cmd

extern uint16_t xp_brush_get_code(void);    //get brush motor error/warn code
extern uint16_t xp_brush_get_state(void);   //get brush motor state
extern float xp_brush_get_current(void);    //get brush motor current
extern float xp_brush_get_torque(void);
extern int xp_brush_get_speed(void);
extern int xp_brush_press_stop_get(void);	//BRUSH press stop state


extern int xp_brush_init(uint8_t port,uint8_t slave_id);    //brush motor init
extern int xp_brush_recover(uint8_t port,uint8_t slave_id);
extern float xp_brush_read_current(void);  //read brush current
extern void xp_brush_start_read_current(u8 sta);    //start read current
extern void xp_brush_current_record(float cunrent_temf);
extern float xp_brush_average_current(u16 index);
extern int xp_brush_debug(char *type,char *fun,char *param);    //brush debug


#endif
//end of the file
